package com.moli.iov.handle.alarm.biz.valid.disarmalarm;

import com.moli.iov.handle.alarm.biz.valid.ValidHandler;
import com.moli.iov.handle.alarm.biz.utils.AlarmUtils;
import com.moli.iov.handle.alarm.constants.Constant;
import com.moli.iov.handle.alarm.model.dto.DisarmAlarmDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <p>
 *  取消报警 防抖 校验
 * </p>
 *
 * @author: zql
 * @Date: 2019/2/28 10:20
 */
@Slf4j
@Component
public class DisarmAlarmAntiShakeHandler implements ValidHandler<DisarmAlarmDto> {

    @Autowired
    private AlarmUtils alarmUtils;

    @Override
    public boolean doHandle(DisarmAlarmDto disarmAlarmDto) {

        if(!disarmAlarmDto.getIsAntiShake()){
            log.info("取消报警 不需要防抖 入参:{}", disarmAlarmDto);
            return true;
        }

        return isDisarmAlarm(disarmAlarmDto);
    }

    /**
     * 判断是否是有效取消报警  防抖
     *
     * @param disarmAlarmDto          车架号
     * @return
     */
    private boolean isDisarmAlarm(DisarmAlarmDto disarmAlarmDto){

        int times = alarmUtils.getAlarmOff(disarmAlarmDto.getVin(),
                disarmAlarmDto.getAlarmCode());
        if(times <= Constant.ANTI_SHAKE_NON){

            alarmUtils.setAlarmOff(disarmAlarmDto.getVin(),
                    disarmAlarmDto.getAlarmCode(),
                    Constant.ANTI_SHAKE_INIT);
            return false;
        }

        Long timesLong = alarmUtils.incrtAlarmOff(disarmAlarmDto.getVin(),
                disarmAlarmDto.getAlarmCode(),
                Constant.ANTI_SHAKE_STEP);

        return timesLong >= Constant.ALARM_TIMES;
    }
}
